const FsExtra = require('fs-extra');
const path = require('path');
const adm_zip = require('adm-zip');

function deleteFolderRecursive(path) {
    if (FsExtra.existsSync(path)) {
        FsExtra.readdirSync(path).forEach((file) => {
            const curPath = path + '/' + file;
            if (FsExtra.lstatSync(curPath).isDirectory()) {
                deleteFolderRecursive(curPath);
            } else {
                FsExtra.unlinkSync(curPath);
            }
        });
        // 删除空目录
        FsExtra.rmdirSync(path);
    }
}

// 调用函数删除目录，替换为你要删除的目录路径
deleteFolderRecursive(path.join(__dirname, 'result', 'public'));
// 定义源目录和目标目录
const sourcePublicDir = path.join(__dirname, 'src', 'public');
const targetPublicDir = path.join(__dirname, 'result', 'public');
// 复制静态文件
FsExtra.copySync(sourcePublicDir, targetPublicDir);


// 将node复制到打包目录
const sourceNodeDir = path.join(__dirname,'src','node');
const targetNodeDir = path.join(__dirname,'result','node');
FsExtra.copySync(sourceNodeDir,targetNodeDir); 



// 打包source模板源码
// 要压缩的source模板目录
const sourceDir = 'src/source';
const outputZipFile = 'result/source.cool';
const devOutputZipFile = 'src/source.cool';

var zip = new adm_zip();
zip.addLocalFolder(sourceDir);
zip.writeZip(outputZipFile);
zip.writeZip(devOutputZipFile);